package com.yuyou.activity.group.service.service.impl;

import com.yuyou.activity.group.service.entity.ActivityGroupJob;
import com.yuyou.activity.group.service.entity.ActivityGroupbuy;
import com.yuyou.activity.group.service.entity.ActivityGroupbuyOrder;
import com.yuyou.activity.group.service.mapper.ActivityGroupbuyMapper;
import com.yuyou.activity.group.service.service.IActivityGroupJobService;
import com.yuyou.activity.group.service.service.IActivityGroupbuyOrderService;
import com.yuyou.activity.group.service.service.IActivityGroupbuyService;
import com.yuyou.activity.group.service.vo.resp.RespCanActivityGroupSPList;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.enums.ActivityGroupBuyEnums;
import com.yuyou.activity.util.enums.ActivityGroupJobEnums;
import com.yuyou.activity.util.util.PhoneUtil;
import com.yuyou.activity.util.util.Sequence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
public class ActivityGroupbuyServiceImpl implements IActivityGroupbuyService {

    @Autowired
    private ActivityGroupbuyMapper mapper;

    @Autowired
    private IActivityGroupbuyOrderService activityGroupbuyOrderService;

    @Autowired
    private IActivityGroupJobService activityGroupJobService;

    @Override
    public int deleteByPrimaryKey(Long activityGroupbuyId) {
        return mapper.deleteByPrimaryKey(activityGroupbuyId);
    }

    @Override
    public int insert(ActivityGroupbuy record) {
    	record.setActivityGroupbuyId(Sequence.getSequence().nextId());
    	if(record.getCreateTime()==null){
        	record.setCreateTime(new Date());    		
    	}
        return mapper.insert(record);
    }

    @Override
    public ActivityGroupbuy selectByPrimaryKey(Long activityGroupbuyId) {
        return mapper.selectByPrimaryKey(activityGroupbuyId);
    }

    @Override
    public int updateByPrimaryKeySelective(ActivityGroupbuy record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public void updateFailActivity() {
        //获取到时失败的团购
        List<ActivityGroupbuy> list = mapper.getFailActivity();

        list.forEach(activityGroupbuy -> {
/*
            //获取该团订单，添加到同步shipin.com的job
            List<ActivityGroupbuyOrder> orders = activityGroupbuyOrderService.findByGroupId(activityGroupbuy.getActivityGroupbuyId());
            if(!CollectionUtils.isEmpty(orders)){
                orders.forEach(o -> {
                    ActivityGroupJob job = new ActivityGroupJob();
                    job.setId(Sequence.getSequence().nextId());
                    job.setfId(o.getActivityGroupbuyOrderId());
                    job.setCreateTime(new Date());
                    job.setType(ActivityGroupJobEnums.type.SHIPIN.getValue());
                    job.setStatus(ActivityGroupJobEnums.status.init.getValue());
                    job.setTag(ActivityGroupJobEnums.tag.SHIPIN.getValue());

                    activityGroupJobService.insert(job);
                });
            }*/

            //更新成差额成功
        	ActivityGroupbuy update=new ActivityGroupbuy();
        	update.setActivityGroupbuyId(activityGroupbuy.getActivityGroupbuyId());
        	update.setStatus(ActivityGroupBuyEnums.status.FAKE_SUCCESS.getValue());
            mapper.updateByPrimaryKeySelective(update);
        });
    }

	@Override
	public ResBoolSimpleInfo<String> add(ActivityGroupbuy record) {
		record.setStatus(ActivityGroupBuyEnums.status.INIT.getValue());
		this.insert(record);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

    @Override
    public List<RespCanActivityGroupSPList> findMoreCanActivityGroupBuy(String projectCode, int limit) {
        List<RespCanActivityGroupSPList> list = mapper.findMoreCanActivityGroupBuy(projectCode , limit);
        //处理手机号码
        list.forEach(respCanActivityGroupSPList -> respCanActivityGroupSPList.setPhone(PhoneUtil.tran4star(respCanActivityGroupSPList.getPhone())));

        list.sort(Comparator.comparing(RespCanActivityGroupSPList::getAttend).reversed().thenComparing(RespCanActivityGroupSPList::getCloseTime));
//        list.sort(Comparator.comparingInt(RespCanActivityGroupSPList::getAttend));

        return list;
    }
}